// 设置健康知识新闻接口
const express = require('express')
const router = express.Router()
const Response = require('../utils/Response.js')

// 引入mysql连接池
const pool = require('../utils/db.js')
const { error } = require('../utils/Response.js')

// 查询所有的健康知识新闻
router.get("/new/all", async (req, resp) => {
  // 获取请求参数   get请求的参数封装在req.query中
  let { page, pagesize } = req.query;
  // 执行查询数组业务
  try {
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from news limit ?,?";
    let result = await pool.querySync(sql, [startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from news";
    let result2 = await pool.querySync(sql2, [startIndex, size]);
    let total = result2[0].count;
    resp.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 按分类查询健康知识新闻
// -- 0-女性健康 1-心脑血管 2-防癌抗癌 3-整形美容 4-饮食营养
// router.post('/newBycategoryId',(req,res) =>{
//   let {n_category,page,pagesize} = req.body
//   let sql = 'select * from news where n_category=? limit ?,?'
//   let startIndex = (page - 1) * 10
//   let size = parseInt(pagesize)
//   pool.query(sql,[n_category,startIndex,size],(err,result) =>{
//     if(err){
//       res.send(Response.error(500,error))
//       throw err
//     }
//     // 将结果返回给客户端
//     res.send(Response.ok(result))
//   })
// })

router.post("/newBycategoryId", async (req, resp) => {
  // 获取请求参数   get请求的参数封装在req.query中
  let { page, pagesize,n_category } = req.body;
  // 执行查询数组业务
  try {
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from news where n_category=? limit ?,?";
    let result = await pool.querySync(sql, [n_category,startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from news where n_category=?";
    let result2 = await pool.querySync(sql2, [startIndex, size,n_category]);
    let total = result2[0].count;
    resp.send(
      Response.ok({ page: parseInt(page), pagesize: size, total, result })
    );
  } catch (error) {
    resp.send(Response.error(error));
  }
});

// 收藏文章
router.post('/new/collect',(req,res) =>{
  let {id,n_id,u_id} = req.body
  let sql = 'insert into new_user (id,n_id,u_id) values (?,?,?)'
  pool.query(sql,[id,n_id,u_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 通过新闻标题模糊查询新闻
router.post('/newLike',(req,res) =>{
  let {n_title} = req.body
  let sql = `select * from news where n_title like '%${n_title}%'`
  pool.query(sql,[n_title],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 通过新闻id查询该新闻详情
router.post('/newById',(req,res) =>{
  let {n_id} = req.body
  let sql = 'select * from news where n_id=?'
  pool.query(sql,[n_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 取消收藏
router.post('/new/delete',(req,res) =>{
  let {id} = req.body
  let sql = 'delete from new_user where id=?'
  pool.query(sql,[id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 新增健康知识新闻
router.post('/new/add',(req,res) =>{
  let {n_id,n_category,n_title,n_details,n_img,n_time} = req.body
  let sql = 'insert into news (n_id,n_category,n_title,n_details,n_img,n_time) values (?,?,?,?,?,?)'
  pool.query(sql,[n_id,n_category,n_title,n_details,n_img,n_time],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 修改健康知识新闻
router.post('/new/update',(req,res) =>{
  let {n_category,n_title,n_details,n_img,n_time,n_id} = req.body
  let sql = 'update news set n_category=?,n_title=?,n_details=?,n_img=?,n_time=? where n_id=?'
  pool.query(sql,[n_category,n_title,n_details,n_img,n_time,n_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

// 删除健康知识新闻
router.delete('/new/delete',(req,res) =>{
  let {n_id} = req.body
  let sql = 'delete from news where n_id=?'
  pool.query(sql,[n_id],(err,result) =>{
    if(err){
      res.send(Response.error(500,error))
      throw err
    }
    // 将结果返回给客户端
    res.send(Response.ok(result))
  })
})

module.exports = router